﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.Entities;
using WXYW.BLL;

namespace WXYW
{
    public partial class UserInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUserInfo();
            }
        }

        private void BindUserInfo()
        {
            customerIDTxtBox.Text = ContextService.CurrentAccount.UserName.ToString();
            if (ContextService.CurrentAccount.UserType.ID != (int)RoleType.Customer)
            {
                passwordTextBox.Text = ContextService.CurrentAccount.UserPassword;
            }            
            customerNameTxtBox.Text = ContextService.CurrentAccount.FullName;
            companyNameTxtBox.Text = ContextService.CurrentAccount.CompanyName;
            contactTxtBox.Text = ContextService.CurrentAccount.ContactPerson;
            phoneTxtBox.Text = ContextService.CurrentAccount.UserPhone;
            mobilePhoneTxtBox.Text = ContextService.CurrentAccount.UserMobilePhone;
            faxTxtBox.Text = ContextService.CurrentAccount.UserFax;
            emailTxtBox.Text = ContextService.CurrentAccount.UserEmail;
            addressTxtBox.Text = ContextService.CurrentAccount.UserAddress;

            ServiceStationService stationService = new ServiceStationService();
            ServiceStationDDL.DataSource = stationService.GetAllWithSelectOne();
            ServiceStationDDL.DataTextField = "Name";
            ServiceStationDDL.DataValueField = "ID";
            ServiceStationDDL.DataBind();
            if (!ContextService.CurrentAccount.ServiceStationReference.IsLoaded)
            {
                ContextService.CurrentAccount.ServiceStationReference.Load();
            }
            if (ContextService.CurrentAccount.ServiceStation != null)
            {
                ServiceStationDDL.SelectedValue = ContextService.CurrentAccount.ServiceStation.ID.ToString();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                UserService userService = new UserService();
                ServiceStationService stationService = new ServiceStationService();
                User user = userService.GetUserByID(ContextService.CurrentAccount.UserID);
                user.FullName = customerNameTxtBox.Text;
                user.CompanyName = companyNameTxtBox.Text;
                user.ContactPerson = contactTxtBox.Text;
                user.UserPhone = phoneTxtBox.Text;
                user.UserMobilePhone = mobilePhoneTxtBox.Text;
                user.UserFax = faxTxtBox.Text;
                user.UserEmail = emailTxtBox.Text;
                user.UserAddress = addressTxtBox.Text;

                if (ContextService.CurrentAccount.UserType.ID != (int)RoleType.Customer && !string.IsNullOrEmpty(passwordTextBox.Text))
                {
                    user.UserPassword = passwordTextBox.Text;
                }
                if (ServiceStationDDL.SelectedValue != "0")
                {
                    user.ServiceStation = stationService.GetServiceStation(Convert.ToInt32(ServiceStationDDL.SelectedValue));
                }
                user.UserName = customerIDTxtBox.Text;
                userService.UpdateUser(user);
                promptLbl.Text = "用户信息已成功更新，请关闭当前页面，重新登录。";
                Response.Write(string.Format("<script language='JavaScript'> alert( '您的信息已成功修改，请重新登录！');top.location.href= 'Default.aspx' </script> ")); 
            }
            catch (Exception ex)
            {
                promptLbl.Text = string.Format("系统错误:{0}。", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, "更新用户资料UserInfo.aspx.cs->SaveButton_Click");
            }
        }
    }
}
